/*Copyright*/
package com.jmetest.physics;

import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;


public class Utils {
    /**
     * Little helper method to color a spatial.
     *
     * @param spatial the spatial to be colored
     * @param color   desired color
     * @param shininess desired shininess
     */
    public static void color( Spatial spatial, ColorRGBA color, int shininess ) {
        DisplaySystem display = DisplaySystem.getDisplaySystem();
        final MaterialState materialState = display.getRenderer().createMaterialState();
        materialState.setDiffuse( color );
        materialState.setAmbient( color.mult( new ColorRGBA( 0.3f, 0.3f, 0.3f, 1 ) ) );
        materialState.setShininess( shininess );
        float mul = 1 + shininess > 18 ? (shininess-28)*0.01f : 0;
        materialState.setSpecular( color.mult( new ColorRGBA( mul, mul, mul, 1 ) ) );
        spatial.setRenderState( materialState );

        if ( color.a < 1 ) {
            final AlphaState alphaState = display.getRenderer().createAlphaState();
            alphaState.setEnabled( true );
            alphaState.setBlendEnabled( true );
            alphaState.setSrcFunction( AlphaState.SB_SRC_ALPHA );
            alphaState.setDstFunction( AlphaState.DB_ONE_MINUS_SRC_ALPHA );
            alphaState.setTestEnabled(true);
            alphaState.setTestFunction(AlphaState.TF_GREATER);
            spatial.setRenderState( alphaState );
            spatial.setRenderQueueMode( Renderer.QUEUE_TRANSPARENT );
        }

        CullState cullState = display.getRenderer().createCullState();
        cullState.setCullMode( CullState.CS_BACK );
        spatial.setRenderState( cullState );
    }
}

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  2007/06/16 14:17:01  irrisor
 * Contacts got an 'applied' flag to avoid application but still generate events -> GHOST material working;
 * New test with dominos, some optimizations concerning enabled nodes; physics speed can be adjusted in SimplePhysicsGame
 *
 */

